<template>
  <div class="xh-list">
    <tab :line-width="2"
         active-color="#DC220B">
      <tab-item :selected="isHistory===false" @on-item-click="changeTab(false)">未读</tab-item>
      <tab-item :selected="isHistory===true" @on-item-click="changeTab(true)">已读</tab-item>
    </tab>
    <div class="content"
         v-for="(receiveItemList,key) in currentPageData"
         :key="key" >
      <div class="data-title">
        {{formatDate(key)}}
      </div>
      <group class="group-main" v-for="(item,key) in receiveItemList"
         :key="key">
        <cell @click.native="receiverJump(item)" is-link >
          <div slot="title"
               class="list-cell-tilte cell-desc">{{item.Subject}}</div>
          <div slot="inline-desc" >
            <span v-html="item.Content"></span>
          </div>
        </cell>
        </cell>
      </group>
    </div>
  </div>
</template>

<script>
import { Search, Tab, TabItem, Group, Cell, XButton, dateFormat } from 'vux'
import { MessageReceiveService } from '../../service/Services.js'
import _ from 'lodash'

export default {
  name: 'CellBtn',
  components: {
    Search,
    Tab,
    TabItem,
    Group,
    Cell,
    XButton
  },
  data () {
    return {
      searchText: '',
      pageData: {},
      isHistory: false
    }
  },
  methods: {
    loadData () {
      this.$vux.loading.show({
        text: '加载中...'
      })
      let providerGUID = this.$store.getters.getUserInfo.ProviderGUID
      MessageReceiveService.GetMessageReceive(providerGUID).then((res) => {
        this.$vux.loading.hide()
        // 分组
        this.pageData = res.Data
      })
      let isHistory = this.$route.query.IsHistory
      // 如果是历史进来 进入到已读
      if (isHistory === 1) {
        this.isHistory = true
      }
    },
    formatDate (myDate) {
      return dateFormat(new Date(myDate), 'YYYY年MM月DD日')
    },
    changeTab (isHistory) {
      this.isHistory = isHistory
    },
    receiverJump (item) {
      let providerGUID = this.$store.getters.getUserInfo.ProviderGUID
      // let providerGUID = '8F923CE0-D0C7-E411-9456-00155D0A6F25'
      let me = this
      // 标志位已读
      MessageReceiveService.SetMessageHistory(item.MsgGUID).then((res) => {
        item.IsHistory = 1
          // 跳转
        let aryBm = ['待报名']
        if (aryBm.includes(item.Subject)) {
            // 跳转招标预告详情
            // 获取获取招标预告GUID
          let cgPlanGUID = item.Url.match(/[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}/gi)[0]
          MessageReceiveService.GetInviteBidForenoticeGUID(cgPlanGUID, providerGUID).then((res) => {
            let path = {path: '/demos/bid-forenotice-invited-detail', query: {InviteBidForenoticeGUID: res.Data[0].InviteBidForenoticeGUID, BmState: '报名中'}}
            me.$router.push(path)
          })
        }
           // 跳转到地铁图
        let aryDtType = ['入围结果通知', '定标结果通知', '发标维护', '待回标', '待下载标书', '下载标书补充文件', '发标情况变更通知']
        if (aryDtType.includes(item.Subject)) {
          let cgPlanGUID = item.Url.match(/[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}/gi)[0]
          MessageReceiveService.GetInviteBidGUID(cgPlanGUID, providerGUID).then((res) => {
            let path = {path: '/demos/my-invite-bid-detail',
              query: {InviteBidGUID: res.Data[0].InviteBidGUID, CgPlanGUID: cgPlanGUID, InviteBidForenoticeGUID: res.Data[0].InviteBidForenoticeGUID}}
            me.$router.push(path)
          })
        }
        // 跳转到答疑界面
        let aryAnswer = ['下载标书回复文档']
        if (aryAnswer.includes(item.Subject)) {
          let InviteBidGUID = item.Url.match(/[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}/gi)[0]
          MessageReceiveService.GetPlanGUIDByInviteBidGUID(InviteBidGUID, providerGUID).then((res) => {
            let path = {path: '/demos/invite-bid-question',
              query: {InviteBidGUID: InviteBidGUID, CgPlanGUID: res.Data[0].CgPlanGUID, InviteBidForenoticeGUID: res.Data[0].InviteBidForenoticeGUID}}
            me.$router.push(path)
          })
        }
        // 跳转到答疑界面
        let arySugest = ['待澄清']
        if (arySugest.includes(item.Subject)) {
          let guids = item.Url.match(/[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}/gi)
          let InviteBidGUID = guids[0]
          let inquiryGUID = guids[2]
          let path = {path: '/demos/invite-bid-ask-detail',
            query: {inviteBidGUID: InviteBidGUID, inquiryGUID: inquiryGUID}}
          me.$router.push(path)
        }
      })
    }
  },
  computed: {
    currentPageData () {
      if (this.isHistory) {
        let hisPageData = _.filter(this.pageData, { 'IsHistory': 1 })
        return _.groupBy(hisPageData, item => {
          return item.ReceiveTime
        })
      } else {
        let hisPageData = _.filter(this.pageData, { 'IsHistory': 0 })
        return _.groupBy(hisPageData, item => {
          return item.ReceiveTime
        })
      }
    }
  },
  mounted () {
    this.loadData()
  }
}
</script>

<style scoped lang="less">
@import '../../assets/less/list.less';
.content{
  .data-title{
    font-family: PingFangSC-Regular;
    font-size: 13px;
    color: #888888;
    letter-spacing: 0;
    text-align: center;
    position: relative;
    top: 10px;
  }
  group-main{
    margin-top: 10px;
  }
}
</style>
